package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.Course;

/**
 * @author 袁小弘
 * @describe 课程DAO层
 * @date 2019-12-31
 */
@Repository
public interface CourseDao extends JpaRepository<Course, Integer> {

    // 根据课程ID ==> 删除课程信息
    @Transactional
    @Modifying
    int deleteByCourseId(Integer courseId);

    // 根据课程ID ==> 查询课程信息
    List<Course> findByCourseId(Integer courseId);

    // 根据课程IDS ==> 批量删除课程信息
    @Transactional
    @Modifying
    int deleteByCourseIdIn(List<Integer> listId);

    // 根据课程名称 ==> 模糊查询
    @Query(nativeQuery = true, value = "SELECT c.*,ct.typeName FROM course c LEFT JOIN course_type ct "
            + "ON c.course_type_Id = ct.course_type_Id  WHERE c.courseName like CONCAT('%',:courseName,'%')")
    List<Map<String, Object>> findByCourseNameLike(@Param("courseName") String courseName);

    // 根据课程学分 ==> 模糊查询
    @Query(nativeQuery = true, value = "SELECT c.*,ct.typeName FROM course c LEFT JOIN course_type ct "
            + "ON c.course_type_Id = ct.course_type_Id  WHERE c.courseCredit like CONCAT('%',:courseCredit,'%')")
    List<Map<String, Object>> findByCourseCreditLike(@Param("courseCredit") String courseCredit);

    // 根据课程课时 ==> 模糊查询
    @Query(nativeQuery = true, value = "SELECT c.*,ct.typeName FROM course c LEFT JOIN course_type ct "
            + "ON c.course_type_Id = ct.course_type_Id WHERE c.courseHour like CONCAT('%',:courseHour,'%')")
    List<Map<String, Object>> findByCourseHourLike(@Param("courseHour") String courseHour);

    // 根据课程类型名称 ==> 模糊查询
    @Query(nativeQuery = true, value = "SELECT c.*,ct.typeName FROM course c LEFT JOIN course_type ct "
            + "ON c.course_type_Id = ct.course_type_Id WHERE ct.typeName like CONCAT('%',:typeName,'%')")
    List<Map<String, Object>> findByTypeNameLike(@Param("typeName") String typeName);

    // 根据课程ID ==> 匹配连接查询所有
    @Query(nativeQuery = true, value = "SELECT c.*,ct.typeName FROM course c LEFT JOIN course_type ct "
            + "ON c.course_type_Id = ct.course_type_Id")
    List<Map<String, Object>> queryAllClassAndTerm();

}
